package com.system.borrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.system.borrow.Constant.URLDefind;
import com.system.borrow.R;
import com.system.borrow.adapter.CardAdapter;
import com.system.borrow.adapter.CardListAdapter;
import com.system.borrow.bean.GloData;
import com.system.borrow.bean.LoanList;
import com.system.borrow.bean.TuiJian;
import com.system.borrow.cache.CacheMode;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.utils.OkHttpUtils;
import com.system.borrow.utils.ListViewUtils;
import com.system.borrow.utils.SecondTitle;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.Request;
import okhttp3.Response;

/**
 * 信用卡推荐
 */
public class CreditActivity extends BaseActivity {
    ListView lv_loan,lv_xyk;
    CardAdapter adapter;
    CardListAdapter xyk_adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credit);
        SecondTitle title = new SecondTitle(this);
        title.setTitle("信用卡",null);
        initView();
        addAction();
        loadData();
        initListener();
    }

    private void initListener() {
        lv_loan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(GloData.getCustomerDTO()==null){
                    Intent loginIntent = new Intent(CreditActivity.this,LoginActivity.class);
                    startActivity(loginIntent);
                    return;
                }
                Intent intent = new Intent(CreditActivity.this, WebViewActivity.class);
               TuiJian tuijian = (TuiJian) adapter.getItem(position);
                intent.putExtra("name",tuijian.name);
                intent.putExtra("url",tuijian.url);
                intent.putExtra("id",tuijian.id);
                intent.putExtra("type",1);
                startActivity(intent);
            }
        });
    }

    private void addAction() {
        adapter = new CardAdapter(CreditActivity.this);
        lv_loan.setAdapter(adapter);
        xyk_adapter = new CardListAdapter(this);
        lv_xyk.setAdapter(xyk_adapter);
    }

    private void loadData() {
        OkHttpUtils.get(URLDefind.XYK_LIST)
                .tag(this)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(new DialogCallback<String>(this,String.class,"加载中...") {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                        if(s!=null){
                            try {
                                JSONObject json = new JSONObject(s);
                                int statusCode = json.getInt("statusCode");
                                if(statusCode==200){
                                    Gson gson = new Gson();
                                    TypeToken<List<LoanList>> token1 = new TypeToken<List<LoanList>>(){};
                                    JSONObject data = json.getJSONObject("data");
                                    List<LoanList> list = gson.fromJson(data.getJSONArray("loanCategoryList").toString(),token1.getType());
                                    xyk_adapter.setList(list);
                                    ListViewUtils.setListViewHeightBasedOnChildren(lv_xyk);
                                    TypeToken<List<TuiJian>> token2 = new TypeToken<List<TuiJian>>(){};
                                    List<TuiJian> dataList = gson.fromJson(data.getJSONArray("dataList").toString(),token2.getType());
                                    adapter.setList(dataList);
                                    ListViewUtils.setListViewHeightBasedOnChildren(lv_loan);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                    }
                });
    }

    private void initView() {
        lv_loan = (ListView) findViewById(R.id.lv_loan);
        lv_xyk = (ListView) findViewById(R.id.lv_xyk);
    }
}
